#!/usr/bin/python

__author__ = 'vinoaj@gmail.com (Vinoaj Vijeyakumaar)'


import logging
import os
from google.appengine.api import users
from google.appengine.ext.webapp import template


class Renderer(object):
  
  def __init__(self, template_filename='index.html', self_uri=''):
    self.template_base_path = os.path.join(os.path.dirname(__file__), 
                                           'templates/')
    self.template_filename = template_filename
    self.self_uri = ''
    self.template_values = {'user'      :None,
                            'logged_in' :False,
                            'login_uri' :'',
                            'logout_uri':''
                            }
    self.initDefaultValues()
    
  def initDefaultValues(self):
    user = users.get_current_user()
    logged_in = True
    login_uri = ''
    logout_uri = ''
    
    if not user:
      logged_in = False
      login_uri = users.create_login_url(self.self_uri)
    else:
      logout_uri = users.create_logout_url(self.self_uri)
      
    self.template_values['user'] = user
    self.template_values['logged_in'] = logged_in
    self.template_values['login_uri'] = login_uri
    self.template_values['logout_uri'] = logout_uri
    
  def render(self):
    template_path = os.path.join(self.template_base_path, 
                                 self.template_filename)
    logging.debug(template_path)
    return template.render(template_path, self.template_values)